//app.js
App({

  onLaunch: function () {
    // 展示本地存储能力
    
    var session_key = wx.getStorageSync('session_key'); 
    if (session_key){
      this.globalData.session_key.userId = session_key[0];
      this.globalData.session_key.salt = session_key[1];
    }
    this.globalData.nickName = wx.getStorageSync("nickName");
    this.globalData.nickName = wx.getStorageSync("avatarUrl");
    //版本更新
    var updateManager = wx.getUpdateManager();

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })
    console.log("版本更新")
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })

    })
    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
      wx.showModal({
        title: '更新提示',
        content: '新版本下载失败，请重试！',
        showCancel:false
      })
    })
  },
  globalData: {
    session_key:{
      userId:0,
      salt:''
    },
    nickName:"",
    avatarUrl:""
  },
  showModal(content) {
    wx.showModal({
      title: "提示",
      content: content,
      showCancel: false
    })
  },
  request(param){
    if (param =="header"){
      return { 'content-type': 'application/x-www-form-urlencoded' };
    } else if (param == "url"){
      //return "https://a03.saicmaxus.com/wgame5/suvconcar/api.php?act=";
      //return "http://192.168.1.99/0320/api.php?act=";
      return "http://192.168.1.86/carApi/api.php?act=";
    } else if (param == "imgUrl") {  
      //return "https://cdn3.maxuscloud.com/wgame5/suvconcar/image66";
      return "http://192.168.1.86/setCar/image0620/image0620";
    } else if (param == "authSession") {
      //return "https://a03.saicmaxus.com/wgame5/suvconcar/auth.php?act=";
      return "http://192.168.1.86/carApi/auth.php?act="
      //return "http://192.168.1.99/0320/api.php?act="
    }
  },
  Refit(){
    return [
      "我的汽车我做主，我的赛车梦已不止是梦",
      "在这条赛道上，你永远只能看着我的背影喊666",
      "老司机马上发车，位置就剩一个啦",
      "我和我的车一样，都不爱低调",
      "闪瞎你的眼，这就是我组装搭配的爱车",
      "没时间解释了，赶快上车",
      "饭后不宜久坐，上车我们去兜风"
    ]
  },
  shareTitle1(){
    return "定制车神重磅来袭！iphone X等多重好礼任性送送送"
  },
  shareTitle2() {
    return "定制车神重磅来袭！iphone X等多重好礼任性送送送"
  },
  //不是从分享页分享出去的，宣言随机一句
  shareFn(ticket){
    var that=this;
    wx.getShareInfo({
      shareTicket: ticket,
      success: function (Re) {
        var encryptedData = Re.encryptedData;
        var iv = Re.iv;
        //请求分享接口加次数
        wx.request({
          url: getApp().request("url") + 'shareGroupAddDrawNum',
          method: "POST",
          data: { userId: getApp().globalData.session_key.userId, salt: getApp().globalData.session_key.salt, encryptedData: encryptedData, iv: iv },
          header: getApp().request("header"),
          success: function (Res) {
            console.log(Res);
            if (Res.data.code == 200) {
              //当日剩余抽取次数减去一
              var shareLeftN = that.globalData.shareLeftN;
              shareLeftN--;
              that.globalData.shareLeftN=shareLeftN;
              //剩余抽取次数加3次
              var drawLeftN = that.globalData.drawLeftN;
              drawLeftN += 3;
              that.globalData.drawLeftN=drawLeftN;
            } else if (Res.data.code == 401 || Res.data.code == 301 || Res.data.code == 302) {
              getApp().showModal("提交信息有误!")
            } else if (Res.data.code ==304) {
              //getApp().showModal("解密错误!")
              that.sessionKey()
            }
          }
        })
      }
    })
  },
  //更新sessionKey
  sessionKey(){
    wx.login({
      success: function (log) {
        console.log(log.code);
        if (log.code) {
          wx.request({
            url: getApp().request("authSession") + 'updateSessionKey',
            method: "POST",
            data: { code: log.code },
            header: getApp().request("header"),
            success: function (res) {
              console.log(res);
              if (res.data.code == 200) {
                console.log("更新成功");
              } else if (res.data.code == 401) {
                console.log("提交信息有误");
              } else if (res.data.code == 301) {
                console.log("小程序code接口无返回");
              } else if (res.data.code == 302) {
                console.log("小程序openid无记录");
              } else if (res.data.code == 303) {
                console.log("更新异常");
              }
            },
            fail: function (fail) {
              console.log(fail)
            }
          })
        }
      }
    })
  }
})
